var router = require('libs/router').router;
var pages = require('libs/pages');
var ItemMixin = require('mixins/item');
var libs = require('libs/libs');
var api = libs.api;
var valide = libs.formValide;
var _user;

var _page = {
    id: "",
    type: ""
}

function bindClick(data){

    var check_item_edit = valide('xxx')
        (data.id, 'username', function(stat, reg){
            if (!stat){
                reg.tips(data.msg);
                return false;
            }
            return stat;
        })

    var postdata = {
        login: _user.login,
        method: 'get'
    }

    $('.uc_edit_item_submit').off('click')
    $('.uc_edit_item_submit').on('click', function(){
        // postdata[data.key] = $('#'+data.id).val()
        check_item_edit(function(query,eles){
            if (query.ckstat){
                postdata[data.key] = query[data.id]
                if (data.key==='invitationCode'){
                    postdata['uniqueIdentifier'] = 'WEIXIN_INPUT'
                }
                api.req(data.api, postdata, function(d, s, x){
                    if (!d.code){
                        router.goback()
                    }
                })
            }
            else {
                libs.msgtips('请正确输入')
            }
        })

    })
}

var bindEvent = function(){
    var type = _page.type.type;
    switch (type) {
        case 'nickname':
            _data = {
                msg: '请正确输入用户名',
                key: 'userName',
                api: "/updataUserName" ,
                id: "uc_edit_item_name"
            }
            bindClick(_data)
            break;
        case 'mobile':
            // _data = {
            //     key: 'mobile',
            //     api: "/updataUserName" ,
            //     id: "uc_edit_item_mobile"
            // }
            // $('#'+_data.id).val(_user.info.mobile)
            // bindClick(_data)
            break;
        case 'address':
            // _data = {
            //     key: 'userName',
            //     api: "/updataUserName" ,
            //     id: "uc_edit_item_address"
            // }
            // $('#'+_data.id).val(_user.info.mobile)
            // bindClick(_data)
            break;
        case 'yqm':
            _data = {
                msg: '请输入正确的邀请码',
                key: 'invitationCode',
                api: "/weixinSaveInvitationCode" ,
                id: "uc_edit_item_yqm"
            }
            bindClick(_data)
            break;
        default:

    }
}

function renderHtml(){
    var me = this;
    var type = _page.type.type;
    var _data = {
        label: "昵称" ,
        id: "uc_edit_item_name",
        value: _user.info.userName
    }
    switch (type) {
        case 'mobile':
            _data = {
                label: "账号" ,
                id: "uc_edit_item_mobile",
                value: _user.info.mobile
            }
            break;
        case 'yqm':
            _data = {
                label: "邀请码" ,
                id: "uc_edit_item_yqm",
                value: _user.info.invitationCode
            }
            break;
        default:

    }
    React.unmountComponentAtNode(document.getElementById(_page.id))
    me.main(me, _data)
}

//演示模块
var Show = React.createClass({
    mixins: [ItemMixin],
    getInitialState: function() {
        return {
            label: "昵称" ,
            id: "uc_edit_name",
            value: ''
        };
    },
    componentWillMount: function() {
        if (this.props.data){
            var _data = this.props.data;
            if (_data.label)
                this.setState({
                    label: _data.label
                })

            if (_data.id)
                this.setState({
                    id: _data.id
                })

            if (_data.value)
                this.setState({
                    value: _data.value
                })
        }
    },
    render:function(){
        return (
            <div className="default_div app_index">
                <ul className="default_Picture">
                    <li>
                        <label>{this.state.label}</label>
                        <input id={this.state.id} type="text" defaultValue={this.state.value} />
                        {/*<i className="iconfont icon-xiangyou"></i>*/}
                    </li>
                </ul>
                <div className="default_foot" style={{backgroundColor:'transparent',marginBottom:"0.2rem"}}>
                    <div className="f_submit" style={{width:'100%'}}>
                        <input type="submit" defaultValue="确定" className="f_btn uc_edit_item_submit" />
                    </div>
                </div>
            </div>
        );
    }
});

function start(name){
    _page.id = name;
    return pages.new({
        boot:function(){

        },
        trigger:function(self, data){
            _user = SA.get('USER')
            _page.type = data
            this.libs.changeTitle('个人资料');    //更改当前页面标题
            renderHtml.call(this)
            // this.main()
        },
        main: function(self, data){
            this.render(
                <Show itemDefaultMethod={bindEvent} data={data}/>,
                document.getElementById(name)
            )
        },
        end: function(){
            React.unmountComponentAtNode(document.getElementById(name))
        }
    })
}

module.exports = start;
